<%-- 
    Document   : adminPage
    Created on : Jul 1, 2012, 1:25:36 PM
    Author     : ThanhTai
--%>

<%@page import="BLO.UserBLO"%>
<%@page import="util.SupportUtil"%>
<%@page import="DTO.User"%>
<%@page import="DTO.Users"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>

<style type="text/css">
    @import url("css/general.css");
</style>
<html>
    <head>
        <script type="text/javascript">
            function validateForm(){
                var form = document.forms["InputUser"];
                var txtUsername = form.txtUsername.value;
                var txtPassword = form.txtPassword.value;
                var txtRePassword = form.txtRePassword.value;
                var txtFullName = form.txtFullName.value;
                if (txtUsername==""){
                    window.alert("Username NOT EMPTY");
                    return false;
                }
                else if (txtPassword==""){
                    window.alert("Password NOT EMPTY");
                    return false;
                }
                else if (txtRePassword==""){
                    window.alert("Retype Password NOT EMPTY");
                    return false;
                }
                else if (txtFullName==""){
                    window.alert("FullName NOT EMPTY");
                    return false;
                }
                else if (txtPassword!=txtRePassword){
                    window.alert("Retype password NOT TRUE");
                    return false;
                }
                return true;
            }
        </script>
        <script type="text/javascript" src="js/Calendar.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Admin Page</title>
    </head>
    <body>
        <div id="wrapper">
            <div id="header">
                <div id="user">
                    ${sessionScope.FULLNAME}, <a href="Controller?action=logout">Log out</a>
                </div>
            </div>

            <div id="body">
                <div id="title">
                    <h2 align="center">Admin Main Page</h2>
                </div>
                <div id="content">
                    <div id="left">
                        <table id="Function Reference:" width="200px">
                            <tr>
                                <th>Main Functions:</th>
                            </tr>
                            <tr>
                                <th><a href="adminMainPage.jsp">Add User</a></th>
                            </tr>
                            <tr>
                                <th><a href="adminAddProject.jsp">Add Project</a></th>
                            </tr>
                        </table>
                    </div>
                    <div id="right">
                        <div id="titleRight"> <h2 align="center">Add User</h2>
                        </div>
                        <div id="contenRight">

                            <jsp:useBean id="userSAXBean" class="DTO.UserAddSAXProcessor"/>
                            <%
                                        String action = request.getParameter("action");
                                        if (action != null) {
                                            if (action.equals("AddUser")) {
                                                try {
                                                    String username = request.getParameter("txtUsername");
                                                    String password = request.getParameter("txtPassword");
                                                    String fullname = request.getParameter("txtFullName");
                                                    String userRole = request.getParameter("ddlRoles");
                                                    request.setAttribute("txtUsername", username);
                                                    request.setAttribute("txtPassword", password);
                                                    request.setAttribute("txtRePassword", password);
                                                    request.setAttribute("txtFullName", fullname);
                                                    request.setAttribute("ddlRoles", userRole);
                                                    UserBLO blo = new UserBLO();
                                                    User user = blo.getUserByUserName(username);
                                                    if (user != null) {
                                                        request.setAttribute("RESULT", "Username have been EXISTED");
                                                    } else {
                                                        String filePath = application.getRealPath("/");
                                                        String fileName = filePath + "xml/Users.xml";
                                                        Users users = new Users();
                                                        user = new User(username, password, fullname, userRole);
                                                        users.add(user);
                                                        SupportUtil util = new SupportUtil();
                                                        util.marshal(fileName, users);
                                                        userSAXBean.save(fileName);
                                                        request.setAttribute("RESULT", "Add User Successful");
                                                    }
                                                } catch (Exception e) {
                                                    System.out.println(e.getMessage());
                                                }
                                                RequestDispatcher rd = request.getRequestDispatcher("adminMainPage.jsp?action=AddUser1");
                                                rd.forward(request, response);
                                            }
                                        }
                            %>
                            <form action="adminMainPage.jsp" method="GET" name="InputUser" onsubmit="return validateForm();">
                                <table border="1">
                                    <tbody>
                                        <tr>
                                            <td>Username: </td>
                                            <td><input type="text" name="txtUsername" value="${requestScope.txtUsername}" /></td>
                                        </tr>
                                        <tr>
                                            <td>Password:</td>
                                            <td><input type="password" name="txtPassword" value="${requestScope.txtPassword}" /></td>
                                        </tr>
                                        <tr>
                                            <td>Re-Password:</td>
                                            <td><input type="password" name="txtRePassword" value="${requestScope.txtRePassword}" /></td>
                                        </tr>
                                        <tr>
                                            <td>Full Name:</td>
                                            <td><input type="text" name="txtFullName" value="${requestScope.txtFullName}" /></td>
                                        </tr>
                                        <tr>
                                            <td>Role:</td>
                                            <td>
                                                <select name="ddlRoles">
                                                    <option value="pm"  <c:if test="${requestScope.ddlRoles}='pm'">selected</c:if>>pm</option>
                                                    <option value="staff" <c:if test="${requestScope.ddlRoles}='staff'">selected</c:if>>staff</option>
                                                </select>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                            </td>
                                            <td>
                                                <input type="submit" name="action" value="AddUser" />
                                                <input type="reset" value="Reset" />
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                                <h4 align="left" style="color: red;">${requestScope.RESULT}</h4>
                            </form>

                        </div>
                    </div>
                </div>
            </div>
            <div id="footer">
            </div>
        </div>
    </body>
</html>
